package com.ruoyi.borrow.controller;

import com.aliyun.oss.OSS;
import com.ruoyi.borrow.entity.Borrow;
import com.ruoyi.borrow.entity.UserInformation;
import com.ruoyi.borrow.service.BorrowService;
import com.ruoyi.borrow.utils.FaceUtils;
import com.ruoyi.common.core.exception.user.Result;
import com.ruoyi.common.core.exception.user.StatusCode;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

/**
 * @Author: 张桦阳
 * @Date: 2023-03-30-15:56:47
 * @Description: 类
 */
@RestController
public class BorrowController {

    @Value("${oss.hostName}")
    private String hostName;

    @Value("${oss.bucket}")
    private String bucket;

    @Resource
    private OSS oss;

    @Resource
    private BorrowService borrowService;
   @RequestMapping("/upload")
    public Result upload(@RequestParam("file") MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        String uuid = UUID.randomUUID().toString();
        //生成新文件名，避免重名文件
        String newFileName = uuid.replace("-","") + filename.substring(filename.lastIndexOf("."));
        oss.putObject(bucket,newFileName,file.getInputStream());
        return Result.success(hostName+newFileName);
    }

    @RequestMapping("/checkFace")
    public Result checkFace(@RequestParam("photo")String photo,@RequestParam("id")Integer id){
        UserInformation userInformation = borrowService.getUser(id);
        Map map = FaceUtils.HumanFace(userInformation.getRealname(), userInformation.getIdCard(), photo);
        String code = (String)map.get("code");
        if(!code.equals("0")){
            String desc = (String)map.get("desc");
            return Result.error(StatusCode.ERROR,desc);
        }
        return Result.success();
    }

}
